<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="rad" uri="http://amc.af.mil/radtaglibrary" %>
<%@ taglib prefix="portal" uri="http://amc.af.mil/portal-el" %>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html-el" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<rad:useBean id="accountViewHelper"/>
<rad:useBean id="adpeAccountViewHelper"/>
<rad:useBean id="authorityViewHelper"/>
<rad:useBean id="organizationViewHelper"/>
<rad:useBean id="rankViewHelper"/>
<c:set var="account" value="${accountViewHelper.account}"/>
<c:set var="authorityIds" value="${authorityViewHelper.authorityIds}"/>
<c:set var="authorityList" value="${authorityViewHelper.all}"/>
<c:set var="authorityMap" value="${authorityViewHelper.authorityIdMap}"/>
<html>
<head>
    <title>Edit User Account</title>
    <link rel="stylesheet" href="yui/build/datatable/assets/skins/sam/datatable-skin.css"/>
    <link rel="stylesheet" href="yui/build/datatable/assets/skins/datatable.css"/>
    <!--<script type="text/javascript" src="yui/build/yuiloader/yuiloader-beta-min.js"></script>-->
    <script type="text/javascript" src="yui/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    <script type="text/javascript" src="yui/build/element/element-beta.js"></script>
    <script type="text/javascript" src="yui/build/json/json.js"></script>
    <script type="text/javascript" src="yui/build/datasource/datasource-beta.js"></script>
    <script type="text/javascript" src="yui/build/datatable/datatable-beta.js"></script>
    <script type="text/javascript" src="yui/build/connection/connection.js"></script>
    <style type="text/css">
        /** Hack to keep an ugly duplicate header from appearing **/
        .yui-dt-hd {
            display: none;
        }

        .yui-dt-db {
            margin-left: auto;
            margin-right: auto;
        }
    </style>
</head>

<body onload="javascript:isLoaded();">

<div id="portal-left-bar">
    <jsp:include page="../../navigation/navigationMenu.jsp"/>
</div>

<div id="portal-context">
<portal:box title="Edit Account" style="width: 800px">
<portal:subSection title="Details">
<div id="standard">
    <html:form method="post" action="accounts/edit.do">
        <html:hidden property="accountId" value="${accountViewHelper.account.id}"/>
        <p>
            <label for="login">Login</label>
            <html:text property="login" styleId="login" readonly="true" value="${account.username}"/>
        </p>

        <p><label for="lastName">Last Name</label>
            <html:text property="lastName" styleId="lastName" value="${account.lastName}"/></p>

        <p>
            <label for="firstName">First Name</label>
            <html:text property="firstName" styleId="firstName" value="${account.firstName}"/>
        </p>

        <p>
            <label for="middleInitial">Middle Initial</label>
            <html:text property="middleInitial" styleId="middleInitial" maxlength="1"
                       value="${account.middleInitial}"/>
        </p>

        <p>
            <label for="rankId">Rank</label>
            <html:select property="rankId" styleId="rankId" value="${account.rank.id}">
                <html:optionsCollection name="rankViewHelper"
                                        property="all"
                                        label="name"
                                        value="id"/>
            </html:select>
        </p>

<%--        <p>
            <label for="organizationId">Organization</label>
            <html:select property="organizationId" styleId="organizationId"
                         value="${account.organization.id}">
                <html:optionsCollection name="organizationViewHelper"
                                        property="all"
                                        value="id"
                                        label="name"/>
            </html:select>
        </p>--%>

        <p>
            <label for="password1">Password</label>
            <html:password property="password1" styleId="password1"/>
        </p>

        <p>
            <label for="password2">Password (Again)</label>
            <html:password property="password2" styleId="password1"/>
        </p>

        <p>
            <label for="role">Role</label> <br/>
            <c:forEach items="${authorityList}" var="authority">
                <c:choose>
                    <c:when test="${authorityMap[authority.id] ne null}">
                        <input type="checkbox"
                               name="authorities"
                               id="authorities"
                               value="<c:out value="${authority.id}" />"
                               checked="true"/>
                    </c:when>
                    <c:otherwise>
                        <input type="checkbox"
                               name="authorities"
                               id="authorities"
                               value="<c:out value="${authority.id}" />"/>
                    </c:otherwise>
                </c:choose>
                <%--<html:multibox property="authorities" value="${authority.id}" />--%>
                <c:out value="${authority.authority}"/>
            </c:forEach>

        </p>

        <p>
            <label for="adpeAccountId">Adpe Account</label>
            <html:select property="adpeAccountId" styleId="adpeAccountId">
                <html:optionsCollection name="adpeAccountViewHelper"
                                        property="all"
                                        value="id"
                                        label="name"/>
            </html:select>
            <button type="button" id="addAdpeAccount">Add Account</button>
        </p>


        <p>
            <div id="parent1" class="yui-skin-sam">
                <div id="adpeAccountTable" style="{width:80%; margin-left: auto; margin-right: auto"></div>
            </div>
        </p>

        <p style="text-align:center">
            <button type="submit">Update</button>
        </p>
    </html:form>
</div>
</portal:subSection>
</portal:box>
</div>
<script type="text/javascript">
    YAHOO.namespace("editAccount.adpeAccountTable");

    YAHOO.editAccount.adpeAccountTable.init = function() {

        var callbacks = {
            success: function(o) {
                var json = YAHOO.lang.JSON.parse(o.responseText);

                var columnDefs = [
                {key: 'id', label: 'ID', formatter: YAHOO.widget.DataTable.formatNumber, width: 200},
                {key:'organization', label: 'Organization', width: 200},
                {key:'name', label:'Account Name', width: 200},
                {key: 'actions', label:'Actions'}
                        ];

                var dataSource = new YAHOO.util.DataSource(json);
                dataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
                dataSource.responseSchema = {
                    resultsList: "results",
                    fields:["id","organization","name"]

                }

                YAHOO.editAccount.adpeAccountTable.dataTable = new YAHOO.widget.DataTable("adpeAccountTable",
                        columnDefs,
                        dataSource,
                {caption:'Assigned ADPE Accounts'});

                YAHOO.util.Event.addListener("addAdpeAccount", "click", function() {

                    //var accountId =<%-- <c:out value="${param.accountId}" />--%>;
                    var accountId = document.getElementById('adpeAccountId').value;

                    var addRowCallbacks = {
                        success: function(o1) {
                            if (accountId > 0) {
                                var addrowJson = YAHOO.lang.JSON.parse(o1.responseText);
                                addrowJson.actions = '<input type="hidden" name="adpeAccounts" value="' + addrowJson.id + '" />';
                                var record = YAHOO.widget.DataTable._cloneObject(addrowJson);
                                YAHOO.editAccount.adpeAccountTable.dataTable.addRow(record);
                            }

                     }, failure: function(o1) {
                        alert(o1.responseText);
                    }
                    }

                    var request = YAHOO.util.Connect.asyncRequest('GET',
                            'adpeAccount/getAdpeAccountById.do?adpeAccountId=' + accountId,
                            addRowCallbacks);


                });
            },
            failure: function(o) {
                alert(o.responseText);
            },
            abort: function(o) {
                alert("Abort!");
            }
        }
        var temp = <c:out value="${param.accountId}" />;
        var request = YAHOO.util.Connect.asyncRequest('GET', 'adpeAccount/getAdpeAccountsByUser.do?accountId=' + temp, callbacks);
    }

    YAHOO.util.Event.onDOMReady(YAHOO.editAccount.adpeAccountTable.init);

</script>
</body>
</html>